package cn.wuweixiang.es.controller;

import cn.wuweixiang.es.model.Model;
import cn.wuweixiang.es.service.ModelService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * The type Model controller.
 *
 * @author weixiang.wu
 */
@RestController
@RequestMapping("/model")
public class ModelController {

    private final static Logger logger = LoggerFactory.getLogger(ModelController.class);
    /**
     * The Model service.
     */
    @Resource
    ModelService modelService;

    /**
     * Save string.
     *
     * @param id   the id
     * @param name the name
     * @return the string
     */
    @GetMapping(value = "/save")
    public String save(Long id, String name) {
        logger.info("Get 请求参数id = [" + id + "] name = [" + name + "]");
        if (id > 0 && StringUtils.isNotEmpty(name)) {
            Model model = new Model(id, name);
            List<Model> addList = new ArrayList<>();
            addList.add(model);
            modelService.saveModels(addList);
            return "Insert the success";
        } else {
            return "Parameter input error";
        }
    }

    /**
     * Save list.
     *
     * @param name the name
     * @return the list
     */
    @GetMapping(value = "/search")
    public List<Model> search(String name) {
        logger.info("Get 请求参数 name = [" + name + "]");
        List<Model> modelList = null;
        if (StringUtils.isNotEmpty(name)) {
            modelList = modelService.searchModel(name);
        }
        return modelList;
    }
}
